package com.tablefood.action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.omg.CORBA.Request;

import com.opensymphony.xwork2.ActionSupport;
import com.tablefood.domain.ShopProduct;
import com.tablefood.service.ProductService;


public class ShowProductsPageAction extends ActionSupport {
 
 
	private int pageIndex = 1;        //当前页
	private int pageSize = 1;         //页面大小
	private int pageCount=1;          //页面总数
	private int startPage=1;          //迭代页码开始位置
	private int endPage=5;            //迭代页码结束位置
	private int productsCount=0;      //商品记录总数
	
	
	private String categoryName=null; //种类名称
	private String productId=null;    //商品Id
	private List<ShopProduct> products;//商品集合
	private Long productNum;            //商品库存
	private Map productPics=null;      //商品图片组
	private int flag=1;                   //图片组判空标志
    private ShopProduct product ;
	
	
	@Resource
	ProductService productService;
	
	public String showProductsPage()
	{
		products=new ArrayList();
		System.out.println("123456");
 
		//通过url传递的参数，中文参数会乱码http://www.cnblogs.com/limeiky/p/5391620.html
        HttpServletRequest request=ServletActionContext.getRequest();
 
//         try {
//			request.setCharacterEncoding("UTF-8");
//			categoryName = new String(request.getParameter("categoryName").getBytes("iso-8859-1"),"utf-8");
//			
//		} catch (UnsupportedEncodingException e) {
//		 
//			e.printStackTrace();
//		}
		
		System.out.println("categoryName:"+categoryName);
		
         
		pageCount=productService.CountProductPage(categoryName,pageSize);

		 //前5页游走情况
		if (pageIndex > 2) {  
            startPage = pageIndex - 2;  
            endPage = pageIndex + 2;  
        }  
		
		//结束页越界控制
        if (endPage > pageCount) {
        	//总页数大于5页，而结束页又大于总页数时
            if (pageCount>5)  
            	//开始页为总页数减4
                startPage = pageCount - 4;
            //总页数少于5页，而结束页又大于总页数时
            else  if(pageCount<5){
             //开始页为1
             startPage = 1;
             //结束页为总页数
             endPage = pageCount;
             }
            //结束页为总页数 
            endPage = pageCount;  
        }
        
        //开始越界控制
        if (startPage < 1) {  
            startPage = 1;  
        }  
        
        //游走控制
        //当前页少于开始+2
        if(pageIndex<startPage+2){
        	//开始页向后游走
        	startPage=startPage-2;
        	endPage=endPage-2;
        	if(startPage<=1){
        		startPage=1;
        		endPage=startPage+4;
        		if(pageCount<endPage)
        			endPage=pageCount;
        	}
        }   
		
        productsCount=productService.getProductsCount(categoryName);
		
        try{                                      
        
        products=productService.searchProducts(categoryName,pageIndex,pageSize);
        }catch(NullPointerException e){
        	e.printStackTrace();
        	
        }
        
        
        	
        /*if(null==products||products.size()==0)
        	return "empty";
        */	
        
		return SUCCESS;
	}
	/**************ItemShop.jsp商品浏览页面控制部分结束**************************/
	
	
	/**************ProductInfo.jsp商品详细页部分********************************/
	public String showProductInfoAction()
	{
		
		product = productService.searchProducts(Long.parseLong(productId));
		 
		//获得库存量，便于js获取
		productNum=product.getNum();
	    try{
	    	
		  //获得图片组
	      	
		  String sourceStr=product.getPics();
		  //逗号分隔
		  String[] sourceStrArray = sourceStr.split(";");
          //将数组放入productPics队列
		  productPics=new HashMap();
		  if(sourceStrArray.length!=0){
		    for (int i = 0; i < sourceStrArray.length; i++) {
              productPics.put(i,sourceStrArray[i]);
            }
		  }else{
			  flag=0;
		  }
	    }catch(java.lang.NullPointerException e){
	    	productPics=new HashMap();
	    	System.out.println("空图片组");
	    } 
	    
	    
    	  
		System.out.println("图片组为："+productPics); 
		System.out.println("封面："+product.getCover());
		return "Info";
	}
	/**************ProductInfo.jsp商品详细页部分结束********************************/
/**************getter,setter*********************/
	
	
	
	
	public int getPageIndex() {
		return pageIndex;
	}
	
	public int getFlag() {
		return flag;
	}

	public void setFlag(int flag) {
		this.flag = flag;
	}

	public Map getProductPics() {
		return productPics;
	}

	public void setProductPics(Map productPics) {
		this.productPics = productPics;
	}

	public Long getProductNum() {
		return productNum;
	}
	public void setProductNum(Long productNum) {
		this.productNum = productNum;
	}
	public String getProductId() {
		return productId;
	}
	public void setProductId(String productId) {
		this.productId = productId;
	}
	public int getProductsCount() {
		return productsCount;
	}
	public void setProductsCount(int productsCount) {
		this.productsCount = productsCount;
	}
	public int getStartPage() {
		return startPage;
	}
	public void setStartPage(int startPage) {
		this.startPage = startPage;
	}
	public int getEndPage() {
		return endPage;
	}
	public void setEndPage(int endPage) {
		this.endPage = endPage;
	}
	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	public String getCategoryName() {
		return categoryName;
	}
	public void setCategoryName(String categoryName) {
		this.categoryName = categoryName;
	}
	public List<ShopProduct> getProducts() {
		return products;
	}
	public void setProducts(List<ShopProduct> products) {
		this.products = products;
	}


	public ShopProduct getProduct() {
		return product;
	}


	public void setProduct(ShopProduct product) {
		this.product = product;
	}
	
 
	
}
